/*
 * @Descripttion: 
 * @Author: cuidm
 * @Date: 2022-07-01 17:35:37
 * @LastEditors: cuidm
 * @LastEditTime: 2022-08-12 12:36:08
 */
import React, { useState, useEffect } from "react";
import { Modal, Form, Button, Input, Switch , Select, message, Space } from "antd";
import { HcpIconSelect } from "@/components";
import cmdbres from "@/data/cmdbres";
import request from "@/utils/request";
import regexpUtil from "@/utils/regexpUtil";
import hcpUtils from "@/utils/hcpUtils";
const user = hcpUtils.getUser();

const CreateCategory = ({ visible, setVisible, onCreateFinish, biz}) => {
	console.log(biz);
	const [form] = Form.useForm();
	const [loading, setLoading] = useState(false);

	const onFinish = async (values) => {
		if(!(biz && biz.cm_unid)) {
			message.warning("请确认是否创建业务！");
			return;
		}
		setLoading(true);
		let res = await request({
			...cmdbres.createServiceCategory,
			data: {
				bizId: biz.cm_unid,
				name: values.name,
				rootId: 0,
				parentId: 0,
				author: user.staffName
			},
		});
		setLoading(false);
		if (res && res.code == 200) {
			setVisible(false);
			message.success("新建分类成功！");
			if (typeof onCreateFinish == "function") onCreateFinish(res);
		} else {
			if(res) message.error(res.msg);
		}
	};

	const onCancel = () => {
		setVisible(false);
	}

	return (
		<div>
			<Modal
				title="新建分类"
				className="hcp-modal-sm"
				visible={visible}
				footer={null}
				onCancel={onCancel}
				destroyOnClose
				maskClosable={false}
				afterClose={()=>{form.resetFields()}}
			>
				<Form
					form={form}
					labelCol={{ span: 5 }}
					wrapperCol={{ span: 18 }}
					name="basic"
					onFinish={onFinish}
				>
					<Form.Item label="分类名称" name="name" rules={[{required: true,message: '请输入分类名称'}]}>
						<Input placeholder="请输入分类名称" maxLength={64} />
					</Form.Item>
					<div className="hcp-modal-body-footer">
						<Form.Item wrapperCol={{ span: 24 }} >
							<Button onClick={onCancel}>取消</Button>
							<Button type="primary" htmlType="submit" loading={loading}>提交</Button>
						</Form.Item>
					</div>
				</Form>
			</Modal>
		</div>
	)
}

export default CreateCategory;